package org.bookr.engine.dao.impl;

import org.bookr.engine.dao.BookrDao;
import org.bookr.engine.model.*;
import org.hibernate.SessionFactory;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Pawel Tomaszewski
 */
public class BookrDaoHibernateImpl implements BookrDao {
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    public Set<Administrator> getAdministrators() {
        sessionFactory.getCurrentSession().beginTransaction();
        Set<Administrator> results = new HashSet<Administrator>(sessionFactory.getCurrentSession().createQuery("from Administrator").list());
        sessionFactory.getCurrentSession().getTransaction().commit();
        return results;
    }
    
    public Administrator getAdministratorById(String id) {
        sessionFactory.getCurrentSession().beginTransaction();
        Administrator result = (Administrator) sessionFactory.getCurrentSession().createQuery("from Administrator where id like '" + id + "'").uniqueResult();
        sessionFactory.getCurrentSession().getTransaction().commit();
        return result;
    }
    
    public Administrator getAdministatorByUsername(String username) {
        sessionFactory.getCurrentSession().beginTransaction();
        Administrator result = (Administrator) sessionFactory.getCurrentSession().createQuery("from Administrator where username like '" + username + "'").uniqueResult();
        sessionFactory.getCurrentSession().getTransaction().commit();
        return result;
    }

    public void deleteAdministrator(Administrator administrator) {
        sessionFactory.getCurrentSession().beginTransaction();
        sessionFactory.getCurrentSession().delete(administrator);
        sessionFactory.getCurrentSession().getTransaction().commit();
    }
    
    public String updateAdministrator(Administrator administrator) {
        sessionFactory.getCurrentSession().beginTransaction();
        Administrator a = (Administrator) sessionFactory.getCurrentSession().merge(administrator);
        sessionFactory.getCurrentSession().getTransaction().commit();
        return a.getId();
    }
    
    public Set<Author> getAuthors() {
        sessionFactory.getCurrentSession().beginTransaction();
        Set<Author> results = new HashSet<Author>(sessionFactory.getCurrentSession().createQuery("from Author").list());
        sessionFactory.getCurrentSession().getTransaction().commit();
        return results;
    }
    
    public Author getAuthor(String id) {
        sessionFactory.getCurrentSession().beginTransaction();
        Author result = (Author) sessionFactory.getCurrentSession().createQuery("from Author where id like '" + id + "'").uniqueResult();
        sessionFactory.getCurrentSession().getTransaction().commit();
        return result;
    }
    
    public void deleteAuthor(Author author) {
        sessionFactory.getCurrentSession().beginTransaction();
        sessionFactory.getCurrentSession().delete(author);
        sessionFactory.getCurrentSession().getTransaction().commit();
    }
    
    public String updateAuthor(Author author) {
        sessionFactory.getCurrentSession().beginTransaction();
        Author a = (Author) sessionFactory.getCurrentSession().merge(author);
        sessionFactory.getCurrentSession().getTransaction().commit();
        return a.getId();
    }
    
    public Set<Book> getBooks() {
        sessionFactory.getCurrentSession().beginTransaction();
        Set<Book> results = new HashSet<Book>(sessionFactory.getCurrentSession().createQuery("from Book").list());
        sessionFactory.getCurrentSession().getTransaction().commit();
        return results;
    }
    
    public Book getBook(String id) {
        sessionFactory.getCurrentSession().beginTransaction();
        Book result = (Book) sessionFactory.getCurrentSession().createQuery("from Book where id like '" + id + "'").uniqueResult();
        sessionFactory.getCurrentSession().getTransaction().commit();
        return result;
    }
    
    public void deleteBook(Book book) {
        sessionFactory.getCurrentSession().beginTransaction();
        sessionFactory.getCurrentSession().delete(book);
        sessionFactory.getCurrentSession().getTransaction().commit();
    }
    
    public String updateBook(Book book) {
        sessionFactory.getCurrentSession().beginTransaction();
        Book b = (Book) sessionFactory.getCurrentSession().merge(book);
        sessionFactory.getCurrentSession().getTransaction().commit();
        return b.getId();
    }

    public Set<Category> getCategories() {
        sessionFactory.getCurrentSession().beginTransaction();
        Set<Category> results = new HashSet<Category>(sessionFactory.getCurrentSession().createQuery("from Category").list());
        sessionFactory.getCurrentSession().getTransaction().commit();
        return results;
    }

    public Category getCategory(String id) {
        sessionFactory.getCurrentSession().beginTransaction();
        Category result = (Category) sessionFactory.getCurrentSession().createQuery("from Category where id like '" + id + "'").uniqueResult();
        sessionFactory.getCurrentSession().getTransaction().commit();
        return result;
    }

    public void deleteCategory(Category category) {
        sessionFactory.getCurrentSession().beginTransaction();
        sessionFactory.getCurrentSession().delete(category);
        sessionFactory.getCurrentSession().getTransaction().commit();
    }
    
    public String updateCategory(Category category) {
        sessionFactory.getCurrentSession().beginTransaction();
        Category c = (Category) sessionFactory.getCurrentSession().merge(category);
        sessionFactory.getCurrentSession().getTransaction().commit();
        return c.getId();
    }
    
    public Set<Comment> getComments() {
        sessionFactory.getCurrentSession().beginTransaction();
        Set<Comment> results =  new HashSet<Comment>(sessionFactory.getCurrentSession().createQuery("from Comment").list());
        sessionFactory.getCurrentSession().getTransaction().commit();
        return results;
    }
    
    public Comment getComment(String id) {
        sessionFactory.getCurrentSession().beginTransaction();
        Comment result = (Comment) sessionFactory.getCurrentSession().createQuery("from Comment where id like '" + id + "'").uniqueResult();
        sessionFactory.getCurrentSession().getTransaction().commit();
        return result;
    }

    public void deleteComment(Comment comment) {
        sessionFactory.getCurrentSession().beginTransaction();
        sessionFactory.getCurrentSession().delete(comment);
        sessionFactory.getCurrentSession().getTransaction().commit();
    }
    
    public String updateComment(Comment comment) {
        sessionFactory.getCurrentSession().beginTransaction();
        Comment c = (Comment) sessionFactory.getCurrentSession().merge(comment);
        sessionFactory.getCurrentSession().getTransaction().commit();
        return c.getId();
    }
    
    public Set<Group> getGroups() {
        sessionFactory.getCurrentSession().beginTransaction();
        Set<Group> results = new HashSet<Group>(sessionFactory.getCurrentSession().createQuery("from Group").list());
        sessionFactory.getCurrentSession().getTransaction().commit();
        return results;
    }

    public Group getGroup(String id) {
        sessionFactory.getCurrentSession().beginTransaction();
        Group result = (Group) sessionFactory.getCurrentSession().createQuery("from Group where id like '" + id + "'").uniqueResult();
        sessionFactory.getCurrentSession().getTransaction().commit();
        return result;
    }

    public void deleteGroup(Group group) {
        sessionFactory.getCurrentSession().beginTransaction();
        sessionFactory.getCurrentSession().delete(group);
        sessionFactory.getCurrentSession().getTransaction().commit();
    }

    public String updateGroup(Group group) {
        sessionFactory.getCurrentSession().beginTransaction();
        Group g = (Group) sessionFactory.getCurrentSession().merge(group);
        sessionFactory.getCurrentSession().getTransaction().commit();
        return g.getId();
    }

    public Set<Reader> getReaders() {
        sessionFactory.getCurrentSession().beginTransaction();
        Set<Reader> results = new HashSet<Reader>(sessionFactory.getCurrentSession().createQuery("from Reader").list());
        sessionFactory.getCurrentSession().getTransaction().commit();
        return results;
    }

    public Reader getReaderById(String id) {
        sessionFactory.getCurrentSession().beginTransaction();
        Reader result = (Reader) sessionFactory.getCurrentSession().createQuery("from Reader where id like '" + id + "'").uniqueResult();
        sessionFactory.getCurrentSession().getTransaction().commit();
        return result;
    }

    public Reader getReaderByUsername(String username) {
        sessionFactory.getCurrentSession().beginTransaction();
        Reader result = (Reader) sessionFactory.getCurrentSession().createQuery("from Reader where username like '" + username + "'").uniqueResult();
        sessionFactory.getCurrentSession().getTransaction().commit();
        return result;
    }

    public void deleteReader(Reader reader) {
        sessionFactory.getCurrentSession().beginTransaction();
        sessionFactory.getCurrentSession().delete(reader);
        sessionFactory.getCurrentSession().getTransaction().commit();
    }

    public String updateReader(Reader reader) {
        sessionFactory.getCurrentSession().beginTransaction();
        Reader r = (Reader) sessionFactory.getCurrentSession().merge(reader);
        sessionFactory.getCurrentSession().getTransaction().commit();
        return r.getId();
    }
}